﻿using Bouyei.GeoCore.Geometries;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bouyei.GeoCore.GeoParsers.GeoJson
{
    public class LineArray : IArray<PointArray>
    {
        private int position = 0;

        private PointArray[] Array;

        public LineArray()
        {

        }

        public LineArray(int capacity)
        {
            Array = new PointArray[capacity];
        }

        public void FromWkt(string wkt)
        {
            Wkt.GeometryReader parser =
                new Wkt.GeometryReader(wkt);

            var ls = parser.ReaderCoodinates();
            if (ls.Count == 0) return;

            var coords = ls[0][0];
            Array = new PointArray[coords.Length];

            for (int i = 0; i < coords.Length; i++)
            {
                Array[i] = new PointArray(coords[i].X, coords[i].Y);
            }
        }

        public void FromArray(Coordinate[] coordinates)
        {
            Array = new PointArray[coordinates.Length];

            for (int i = 0; i < coordinates.Length; i++)
            {
                Array[i] = new PointArray(coordinates[i].X, coordinates[i].Y);
            }
        }

        public object Current => Array[position];

        public PointArray this[int index]
        {
            get { return Array[index]; }
            set { Array[index] = value; }
        }

        public bool MoveNext()
        {
            ++position;
            return position < Array.Length;
        }

        public void Reset()
        {
            position = 0;
        }

        public IEnumerator<PointArray> GetEnumerator()
        {
            foreach (var item in Array)
                yield return item;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            foreach (var item in Array)
                yield return item;
        }
    }
}
